package com.example.qxfw.dataBaseManager.nyzjk.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.qxfw.common.entity.ny.StationInfo;
import com.example.qxfw.common.mapper.StationInfoMapperXml;
import com.example.qxfw.dataBaseManager.nyzjk.service.NyfwzdkService;
import com.example.qxfw.mongodb.hy.entity.HyImageFile;
import com.example.qxfw.mongodb.hy.service.HyImageFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/7/6  星期四   2:23
 * @description : 此类用来
 ***************************************/
@Service
public class NyfwzdkServiceImpl implements NyfwzdkService {

    @Autowired
    private StationInfoMapperXml stationInfoMapperXml;

    @Autowired
    private HyImageFileService hyImageFileService;

    @Override
    public List<StationInfo> getAllOrder() {
        return stationInfoMapperXml.selectOrderByStationId();
    }

    @Override
    public List<StationInfo> selectByStationId(final String stationId) {
        StationInfo stationInfo = new StationInfo();
        stationInfo.setStationid(stationId);
        return this.stationInfoMapperXml.querySelect(stationInfo, null, null);
    }

    @Override
    public Integer saveStationInfo(final StationInfo stationInfo) {
        int insertSelective = this.stationInfoMapperXml.insertSelective(stationInfo);
        return insertSelective;
    }

    @Override
    public Integer updateStationInfo(StationInfo stationInfo) {
        String stationId = stationInfo.getStationid();
        stationInfo.setStationid(null);
        return stationInfoMapperXml.update(stationInfo, new LambdaUpdateWrapper<StationInfo>()
                        .eq(StationInfo::getStationid, stationId)
        );
    }

    @Override
    public Integer deleteStationInfo(final String stationid) {
        return this.stationInfoMapperXml.deleteById(stationid);
    }

    @Override
    public HyImageFile saveStaImage(MultipartFile staImage, String stationid) throws IOException {
        HyImageFile hyImageFile = new HyImageFile();
        hyImageFile.setTable_name("station_info");
        hyImageFile.setData(staImage.getBytes());
        hyImageFile.setName(stationid + "农服站");
        hyImageFile.setId(stationid);
        return hyImageFileService.save(hyImageFile);
    }

    @Override
    public Integer importStationInfo(List<StationInfo> stationInfoList) {
        this.stationInfoMapperXml.clearStationInfo();
        int count = 0;
        for (StationInfo stationInfo : stationInfoList) {
            count += this.stationInfoMapperXml.insert(stationInfo);
        }
        return count;
    }
}

